package com.mobi.Survey.uicomponents;

import java.util.StringTokenizer;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.mobi.Survey.controller.SurveyDetailPojo;

/**
 * 
 * <p>
 * <b>Author:</b> Yahya Arshad
 * <p>
 * <b>Company:</b> Mobi Media Soft
 * <p>
 * <b> Website:</b> www.mobimediasoft.com
 * <p>
 * CustomCheckedBox.java
 * 
 * 
 */
public class CustomCheckedBox extends CheckBox
{

	public static final String DELIM = "|";
	private LinearLayout ll;
	private Context ctx;

	public CustomCheckedBox(Context context, String txt)
	{
		super(context);
		this.setText(txt);
	}

	public void setTextColor(SurveyDetailPojo sdp)
	{
		if (null != sdp && null != sdp.getSurvey()
				&& null != sdp.getSurvey().getFontColor())
		{
			String txtColor = sdp.getSurvey().getFontColor();

			this.setTextColor(Color.parseColor("#" + txtColor));
		} else
		{
			this.setTextColor(Color.WHITE);
		}
	}

	public CustomCheckedBox(Context context, SurveyDetailPojo sdp,
			boolean editable)
	{
		super(context);
		this.ctx = context;
		ll = new LinearLayout(ctx);

		ll.setOrientation(LinearLayout.VERTICAL);
		int index = 0;
		if (sdp.getControlText() != null && sdp.getControlText().length() > 0)
		{
			CustomTextView txt = new CustomTextView(ctx);
			txt.setCustomColor(sdp);
			txt.setText(sdp.getControlText());
			ll.addView(txt);
			index++;

		}
		String vals = sdp.getControlParameter();
		if (vals == null)
			vals = DELIM;

		StringTokenizer stCbs = new StringTokenizer(vals, DELIM);

		if (stCbs != null)
		{

			while (stCbs.hasMoreTokens())
			{
				String cbTxt = stCbs.nextToken();
				if (cbTxt.length() > 0)
				{
					CustomCheckedBox ccb = new CustomCheckedBox(ctx, cbTxt);
					ccb.setTextColor(sdp);
					if (!editable)
						ccb.setClickable(false);
					ll.addView(ccb);
				}

			}
			StringTokenizer stUserVals = new StringTokenizer(
					sdp.getUserValue(), DELIM);
			int oldIndex = index;
			int tChild = ll.getChildCount();
			while (stUserVals.hasMoreElements())
			{
				String token = stUserVals.nextToken();
				if (token.length() > 0)
				{
					while (index < tChild)
					{
						CustomCheckedBox ccb = (CustomCheckedBox) ll
								.getChildAt(index);
						if (ccb.getText().toString().equalsIgnoreCase(token))
						{
							ccb.setChecked(true);
						}

						index++;
					}
				}
				index = oldIndex;
			}
		}

	}

	public View getView()
	{
		return ll;
	}
}
